<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true">
      <el-form-item label="关键字">
        <el-input v-model="key" placeholder="姓名/手机/身份证" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()"> 清空 </el-button>
    </el-form>
    <!-- 列表 -->
    <el-table :data="list" stripe>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="mobile" label="手机" />
      <el-table-column prop="idCard" label="身份证号" width="200" />
      <el-table-column label="性别" width="80">
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column label="是否结婚" prop="params.marry" width="120">
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template scope="scope">
          <el-tag>{{ scope.row.params.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/borrower/detail/' + scope.row.id">
            <el-button v-if="scope.row.status === 1" type="warning" size="mini">
              审批
            </el-button>
            <el-button v-else type="primary" size="mini"> 查看 </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[2, 3, 4, 5, 6, 7, 8, 20]"
      style="padding: 30px 0"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import borrowerApi from "@/api/borrower";
export default {
  data() {
    return {
      key: null,
      //分页数据集合
      list: [],
      page: 1,
      limit: 3,
      total: 0,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //页码改变时的回调
    changeCurrentPage(pageNum) {
      this.page = pageNum;
      this.fetchData();
    },
    //pageSize改变时的回调
    changePageSize(size) {
      this.limit = size;
      this.fetchData();
    },
    //搜索表单提交按钮的事件函数
    fetchData() {
      borrowerApi.list(this.page, this.limit, this.key).then((r) => {
        console.log(r.data.page);
        this.list = r.data.page.records;
        this.total = Number(r.data.page.total);
      });
    },
    //搜索表单清空按钮事件函数
    resetData() {
      this.key = null;
      this.page = 1;
      this.fetchData();
    },
  },
};
</script>
